package org.dant.ant.extension.net.processors;

import java.io.*;
import java.util.*;

import org.dant.ant.extension.net.DefaultDispatcher;
import org.dant.ant.extension.net.Processor;
import org.dant.ant.extension.net.repository.ProcessGroup;
import org.dant.ant.extension.tasks.commands.CommandsConstants;
import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.tasks.internal.RemoteResponse;
import org.dant.ant.extension.utils.ParameterUtil;

public class ExecProcessor extends Executable implements Processor {

    protected StartCxxApp setup(RemoteCommand rcmd) {
        String executable = (String) rcmd.getParameter("executable");
        String[] args = (String[])rcmd.getParameter("arguments");
        boolean blocking = ((Boolean)rcmd.getParameter("blocking")).booleanValue();
    
        StartCxxApp cxxApp = new StartCxxApp();

        cxxApp.setDir((File)rcmd.getParameter("dir"));
        cxxApp.setExecutable(executable);
        cxxApp.setExpectedList((List)rcmd.getParameter("expectedlist"));
        cxxApp.setArguments(args);
        cxxApp.setEnvironment((String[])rcmd.getParameter("enviroment"));

        cxxApp.setBlocking(blocking);
        cxxApp.setUnique(((Boolean)rcmd.getParameter("unique")).booleanValue());
        cxxApp.setDuplicate(((Integer)rcmd.getParameter("duplicate")).intValue());
        cxxApp.setTimeout((Long)rcmd.getParameter("timeout"));
        cxxApp.setFailonerror(((Boolean)rcmd.getParameter("failonerror")).booleanValue());
        return cxxApp;
    }
  
    private void execute(RemoteCommand rcmd, RemoteResponse response) {
        StartCxxApp cxxApp = setup(rcmd);
    
        String key = cxxApp.getProcessKey();
        ProcessGroup group = DefaultDispatcher.getProcessRepository().getProcessGroup(key);
    
        if (group == null) {
            group = new ProcessGroup();
            group.setCommandLine(key);
            group.setProjectName((String)rcmd.getParameter("projectname"));
            group.setTargetName((String)rcmd.getParameter("targetname"));
            group.setTaskName((String)rcmd.getParameter("taskname"));
      
            group.setTestSuiteName((String)rcmd.getParameter("testsuitename"));
            group.setTestCaseName((String)rcmd.getParameter("testcasename"));
      
            group.setSourceHost(rcmd.getSourceHost());
            group.setBlocking(cxxApp.getBlocking());
            DefaultDispatcher.getProcessRepository().addProcessGroup(key, group);
        }
    
        cxxApp.setProcessGroup(group);
        cxxApp.start(response);
    }
  
    public void process(RemoteCommand req, RemoteResponse response) {
        System.out.println("Exec requied from : " + req.getSourceHost());
        response.setCommandType(req.getName());
        if (CommandsConstants.DANT_CMD_EXEC.equals(req.getName())) {
            execute(req, response);
        } else {
            response.setSuccess(false);
            response.setResult("Required a EXEC command , but is not a EXEC command");
        }
    }


}
